package gruppe7.control;

import gruppe7.model.Model;
import gruppe7.model.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Session {
	
	public static String UserKey = "user";
	
	private HttpSession servletSession = null;
	
	public Session(HttpServletRequest request, HttpServletResponse response) {
		servletSession = request.getSession();
		
		if (servletSession.getAttribute(UserKey) == null) {
			String requestUser = request.getUserPrincipal().getName();
			
			try {
				User user = Model.getInstance().getUserByName(requestUser);
				servletSession.setAttribute(UserKey, user);
			}
			
			catch (Exception e) {
				ServletController.getInstance().sendToErrorPage(response);
			}
		}
	}
	
	public User getUser() {
		return (User) servletSession.getAttribute(UserKey);
	}
	
	public void invalidate() {
		servletSession.invalidate();
	}
	
}
